package com.wanmait.happyheating.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.happyheating.pojo.Admin;
import com.wanmait.happyheating.pojo.Userinfo;
import com.wanmait.happyheating.pojo.UserinfoLoginRecord;
import com.wanmait.happyheating.service.UserinfoLoginRecordService;
import com.wanmait.happyheating.service.UserinfoService;
import com.wanmait.happyheating.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;

/**
郭振鹏 2021 05 19
 */

@RestController
@RequestMapping("/user")
public class UserinfoController
{
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private UserinfoLoginRecordService userinfoLoginRecordService;


    /**
     *  //登录方法
    *有token注解不拦截
     */
    @IgnoreToken
    @RequestMapping("login")
    public Result login(@RequestBody Userinfo userinfo ,HttpServletRequest request)
    {
        System.out.println("ooooooooooo"+userinfo.getName());
//     String ip= IPUtils.getIpAddr(request);


/**
 *
 *  Userinfo userinfoLogin=userinfoService
 *    用用户名和密码查找用户如果找到了返回token如果没找到返回erro
 *
 */
      LambdaQueryWrapper<Userinfo> userinfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
      userinfoLambdaQueryWrapper.eq(Userinfo::getName,userinfo.getName());
      userinfoLambdaQueryWrapper.eq(Userinfo::getPass,userinfo.getPass());
    Userinfo userinfoLogin=  userinfoService.getOne(userinfoLambdaQueryWrapper);
  if(userinfoLogin!=null){
/**
 *
 *  Userinfo userinfoLogin=userinfoService
 *    用用户名和密码查找用户如果找到了返回token如果没找到返回erro
 *     //如果登录成功把日志写进数据库  把token传到前端
 */
      UserinfoLoginRecord userinfoLoginRecord=new UserinfoLoginRecord();
      userinfoLoginRecord.setLoginIp("ip");
      userinfoLoginRecord.setUserinfo(userinfoLogin);
      userinfoLoginRecord.setUserinfoId(userinfoLogin.getId());
      userinfoLoginRecord.setLoginTime(new Timestamp(System.currentTimeMillis()));
      userinfoLoginRecordService.save(userinfoLoginRecord);
      String token = UserinfoJWTUtils.createToken(userinfoLogin);
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("token",token);
      return Result.success("登录成功",jsonObject);
  }
        else{
            return Result.success("登录失败没有此用户");
  }

    }

    /**
     *
     * 注册
     * @param userinfo
     * @return
     */
    @IgnoreToken
    @RequestMapping("regist")
    public Result regist(@RequestBody Userinfo userinfo)
    {
        System.out.println("userinfo="+userinfo.getName());

        if(userinfo.getProfile()==null||userinfo.getProfile()==""){
            userinfo.setProfile("user.jpg");
        }
        /**
         *
         * MD5加密
         */
        String MD5Pass=MD5Utils.md5(userinfo.getPass());
        userinfo.setPass(MD5Pass);
        /**
         *
         * 存放到数据库
         */
        userinfoService.save(userinfo);
        return Result.success("注册成功请登录");
    }

}
